
package com.xbongbong.admin.model.impl;

import com.xbongbong.admin.domain.dao.RaidersDao;
import com.xbongbong.admin.domain.entity.RaidersEntity;
import com.xbongbong.admin.model.RaidersModel;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.pro.constant.RaidersConstant;
import com.xbongbong.pro.enums.raiders.enums.RaidersPermissionEnum;
import com.xbongbong.pro.enums.raiders.enums.RaidersPublishEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;


/**
* @author autoGen
* @time 2019-09-18 13:56:16
*/
@Service("raidersModel")
public class RaidersModelImpl implements RaidersModel {

	@Resource
	private RaidersDao dao;

	@Override
	public Integer insert(RaidersEntity entity){
		long now = DateUtil.getInt();
		entity.setAddTime(now);
		entity.setUpdateTime(now);

		return dao.insert(entity);
	}
	@Override
	public Integer update(RaidersEntity entity){
		long now = DateUtil.getInt();
		entity.setUpdateTime(now);

		return dao.update(entity);
	}

	@Override
	public Integer save(RaidersEntity entity){

		if(entity.getId() == null || Objects.equals(0L, entity.getId())) {
			return insert(entity);
		}
		return update(entity);
	}

	@Override
	public void insertBatch(List<RaidersEntity> entityList) {
    	dao.insertBatch(entityList);
    }

	@Override
	public Integer deleteByKey( Long key){
		return dao.deleteByKey(key);
	}

	@Override
	public RaidersEntity getByKey( Long key){
		return dao.getByKey(key);
	}

	@Override
	public List<RaidersEntity> findEntitys(Map<String ,Object>  param){
		return dao.findEntitys(param);
	}

    @Override
	public Integer getEntitysCount(Map<String ,Object>  param){
		return dao.getEntitysCount(param);
	}

	@Override
	public Integer getCountByGroupId(Long groupId) {
		return dao.getCountByGroupId(groupId);
	}

	@Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
	public Integer updateSort(List<RaidersEntity> list) {
		return dao.updateSort(list);
	}

	/**
	 * 批量更新
	 * @author xingxing.xiao
	 * @date 2019/12/27 10:14
	 */
	@Override
	public Integer updateSetBatch(List<RaidersEntity> list) {
		return dao.updateSetBatch(list);
	}

	@Override
	public List<Long> getIdList(Map<String, Object> param) {
		return dao.getIdList(param);
	}

	@Override
	public List<RaidersEntity> getRaidersList4Filter(boolean white, Collection<Long> groupIdIn, List<Integer> stageCodeIn, Integer dataPermission) {
		if (groupIdIn == null || groupIdIn.size() == 0) {
			return new ArrayList<>();
		}
		Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
		map.put("del", 0);
		map.put("groupIdIn", groupIdIn);
		if (!white) {
			//只有非白名单的公司才限制发布、阶段
			if (stageCodeIn == null || stageCodeIn.size() == 0) {
				return new ArrayList<>();
			}
			map.put("publish", RaidersPublishEnum.PUBLISH.getCode());
			map.put("stageCodeIn", stageCodeIn);
			//根据用户权限，过滤掉角色权限不包含该用户权限的攻略
			List<Integer> permissionCodeIn = RaidersPermissionEnum.getPermission(dataPermission);
			map.put("permissionCodeIn", permissionCodeIn);
		}
		map.put("columns", "id,name,instruction,group_id,point,star,`lock`,stage,publish,total_score, point_card");
		map.put("orderByStr", RaidersConstant.ORDER_BY_STR_LIST);
		return findEntitys(map);
	}

	@Override
	public List<Long> getPublishRaidersIdList() {
		Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
		map.put("del", 0);
		map.put("publish", RaidersPublishEnum.PUBLISH.getCode());
		return getIdList(map);
	}
}

